<?php
echo $this->Html->script('jquery.ui.core.min');
echo $this->Html->script('jquery.validate.min');
echo $this->Html->script('jquery.ui.datepicker.min');
echo $this->Html->css('jquery-ui');
?>
<script type="text/javascript">

    $(document).ready(function(){
        $("form").validate({
            errorClass: "jqueryError",
            errorElement: 'label',
            debug: false,
            submitHandler: function(form) {
                $(':submit', form).attr('disabled', 'disabled').addClass('inactive');
                form.submit();
            }
        });

        $('#PotentialProductId').change(function(){
            var $response = $('#response');
            var $btnSubmit = $(':submit');
            if($(this).val() != '')
            {
                $.ajax({
                    type: 'get',
                    url: '<?php echo $this->Html->url(array('controller' => 'potentials', 'action' => 'alreadyapotential')); ?>',
                    data: {
                        userid: $('#PotentialUserId').val(),
                        productid: $('#PotentialProductId').val()
                    },
                    dataType: 'json',
                    success: function(data)
                    {

                        if(data)
                        {
                            $response.show()
                            .attr('style', '')
                            .attr('style', "color:red;")
                            .html('The user "'+data[0].User.firstname+" "+data[0].User.lastname+'" is already a "'+data[0].Potential.type+'" for "'+data[0].Product.name+'"');
                            $btnSubmit.attr('disabled',true).addClass('inactive');
                        }
                        else
                        {
                            $btnSubmit.attr('disabled',false);
                        }
                    }
                });
            }
            else
            {
                $response.html('').hide();
            }
        });
    });

    $(function(){
        $('#PotentialDateenquired').datepicker({
            defaultDate: new Date(),
            minDate: '-1M',
            maxDate: '0D',
            changeMonth: true,
            changeYear: true,
            dateFormat: 'dd-mm-yy',
            showOn: 'both',
            buttonImage: '<?php echo APP_URL; ?>img/calendar.png',
            buttonImageOnly: true
        });
    })
</script>
<?php
echo $this->Form->input('user_id', array(
    'label' => 'User',
    'type' => 'select',
    'options' => $users,
    'class' => 'required',
    'between' => $this->Html->tag('span', '(Choose a user from the list below)', array('class' => 'description'))
));

echo $this->Form->input('product_id', array(
    'label' => 'Product',
    'type' => 'select',
    'options' => $products,
    'empty' => 'Select',
    'class' => 'required',
    'between' => $this->Html->tag('span', '(Choose a product from the list below)', array('class' => 'description'))
));

echo $this->Html->div('', '', array(
    'id' => 'response', 'style' => 'display:none'
));

echo $this->Form->input('type', array(
    'label' => 'Potential Type',
    'type' => 'select',
    'options' => array('lead' => 'Lead', 'account' => 'Account', 'contact' => 'Contact'),
    'empty' => 'Select',
    'class' => 'required',
    'between' => $this->Html->tag('span', '(Choose the type of potential from the list below)', array('class' => 'description'))
));

echo $this->Form->input('dateenquired', array(
    'label' => 'Date Enquired',
    'type' => 'text',
    'class' => 'required',
    'between' => $this->Html->tag('span', '(Choose date on which the enquiry is made)', array('class' => 'description'))
));

echo $this->Form->input('description', array(
    'label' => 'Description',
    'between' => $this->Html->tag('span', '(A little description about this potential)', array('class' => 'description'))
));

echo $this->Form->input('leadstage_id', array(
    'type' => 'select',
    'options' => $leadstages,
    'empty' => 'Select',
    'label' => 'Lead Stage',
    'class' => 'required',
    'between' => $this->Html->tag('span', '(Choose a lead stage)', array('class' => 'description'))
));

echo $this->Form->input('dealstage_id', array(
    'type' => 'select',
    'options' => $dealstages,
    'empty' => 'Select',
    'label' => 'Deal Stage',
    'class' => 'required',
    'between' => $this->Html->tag('span', '(Choose a deal stage)', array('class' => 'description'))
));

echo $this->Form->input('employee_id', array(
    'type' => 'hidden',
    'value' => $this->Session->read('Auth.Employee.id')
));

echo $this->Form->input('assignedtoemployee_id', array(
    'type' => 'select',
    'options' => $assignedtoemployees,
    'empty' => 'Select',
    'label' => 'Assigned To',
    'class' => 'required',
    'between' => $this->Html->tag('span', '(Select an employee from the list below to which this potential be assigned)', array('class' => 'description'))
));

echo $this->Form->input('location_id', array(
    'type' => 'select',
    'options' => $locations,
    'empty' => 'Select',
    'label' => 'Location',
    'class' => 'required',
    'between' => $this->Html->tag('span', '(Select a location where this potential will be handled)', array('class' => 'description'))
));

if( $this->Session->read('Auth.Employee.id') == Configure::read('employeetype.admin.id') )
{
    echo $this->Form->input('status', array(
        'label' => 'Status',
        'checked' => true,
        'after' => $this->Html->tag('span', '(Uncheck to set the status as "disabled")', array('class' => 'description'))
    ));
}
?>